package com.jf.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-20
 * Time:0:35
 */
public class Star {
    //private final BufferedImage image;
    private BufferedImage image;
    private final int x;
    private final int y;
    private final int width;
    private final int height;
    private final int brightness;
    private int alpha;

    private final boolean isTwinkle;
    private boolean isFading;
    private final int speed;

    public enum Grade {
        SMALL,
        MEDIUM,
        BIG
    }

    public Star(int x, int y, Grade grade) {
        Random rand = new Random();
        this.x = x;
        this.y = y;
        switch (grade) {
            case SMALL:
                width = height = 2;
                break;
            case MEDIUM:
                width = height = 3;
                break;
            case BIG:
                width = height = 4;
                break;
            default:
                width = height = 0;
        }
        image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

        isTwinkle = rand.nextBoolean();
        isFading = true;
        speed = rand.nextInt(1,5) * 3;

        Graphics2D graphics = image.createGraphics();
        graphics.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.TRANSLUCENT);
        graphics.dispose();
        graphics = (Graphics2D)image.getGraphics();
        brightness = rand.nextInt(70,256);
        alpha = brightness;
        graphics.setColor(new Color(255,255,255,alpha));
        graphics.fillOval(0,0,width,height);
        graphics.dispose();
    }

    public void Fading() {
        if (isTwinkle) {
            //System.out.println("haha");
            if (isFading) {
                alpha -= speed;
                if (alpha <= 0) {
                    alpha = 0;
                    isFading = false;
                }
            } else {
                alpha += speed;
                if (alpha >= brightness) {
                    alpha = brightness;
                    isFading = true;
                }
            }
            image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
            Graphics2D graphics = image.createGraphics();
            graphics.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            graphics.dispose();
            graphics = (Graphics2D) image.getGraphics();
            graphics.setColor(new Color(255, 255, 255, alpha));
            graphics.fillOval(0, 0, width, height);
            graphics.dispose();
        }
    }

    public boolean isTwinkle() {
        return isTwinkle;
    }
    public void draw(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(image, x, y, null);
    }
}
